import axios from 'axios'
import qs from 'qs'

//let baseUrl = 'http://www.math80.com';
let baseUrl = 'http://www.cmarket-api.com:808';
//let lingPai='WdmxLMvCngT8O5zKUpXICH0ZOdCjot5qTl1rdk4T'
//let baseUrl = 'http://111.230.197.195:808';
let lingPai='Mi2y$10$2EWnVYGTqunVafKUcT4oPrfYNxIQZRFn'
let tokenUrl='/token';
/*if (process.env.NODE_ENV == 'production') {
  baseUrl = '';
}*/

/****************************共用接口****************************/
export const Post = (url,token,data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      baseURL: baseUrl+url,
      headers: {Authorization:"Bearer "+token},
      data: qs.stringify(data)
    }).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.response)
    })
  })
}

//登录
export const Login = (data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      baseURL: baseUrl+'/token',
      data: data
    }).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.response)
    })
  })
}

//注册
export const Reg = (url,data,token) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      baseURL: baseUrl+url,
      headers: {Authorization:"Bearer "+token},
      data: data
    }).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.response)
    })
  })
}

//获取token
export const GetToken = () => {
  return new Promise((resolve, reject) => {
    let data={
        "grant_type": "client_credentials",
        "client_id": "app",
        "client_secret": lingPai,
        "scope": "app"
    }
    axios({
      method: 'POST',
      baseURL: baseUrl+tokenUrl,
      data:data
    }).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.response)
    })
  })
}

export const Get = (url,token) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      baseURL: baseUrl+url,
      headers: {Authorization:"Bearer "+token}
    }).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.response)
    })
  })
}


//修改
export const Put = (url,token,data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'put',
      baseURL: baseUrl+url,
      headers: {Authorization:"Bearer "+token},
      data:data
    }).then((res) => {
      resolve(res)
    }).catch((err) => {
      reject(err.response)
    })
  })
}

//删除
export const Delete = (url,token) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'delete',
      baseURL: baseUrl+url,
      headers: {Authorization:"Bearer "+token}
    }).then((res) => {
      resolve(res)
    }).catch((err) => {
      console.log(err)
      reject(err.response)
    })
  })
}


/*
 *  1.注册接口
 *  @params {String} userPassword 加密
 *  @params {String} referrals 代理商代码，当用户类型是3商家时，可选填
 */
/*export const get = (url,token) => {
  return ajax('/xk_api_user/api/user/regUser',{userPassword, mobile, code, userType, referrals})
}*/